
import api from '../api/index'
import common from '../api/common'
import { userInfoStore } from '../store/userInfo'

const app = getApp()

Page({

  data: {
    userLevel:0,
    menuButtonClientRect:{},
    navigationBarClientRect:{},
    windowClientRect:{},
    banners: [],
    fourCircle:[],
    twoCol: [],
    bgUrl:null,
    videoUrl:null,
    videoTitle:'',
    iphonex: app.globalData.windowClientRect.height == 808 || app.globalData.windowClientRect.height==724,
    selectHotIndex:9,
    successImage:'https://www.daikin1995.com/upload/2020/9/20200915131825.gif',
    selectItem:{},
    hotSearch:[
      { titleWords: '除菌净化', image: 'https://www.daikin1995.com/upload/2020/9/20200904113211.png', link: 'https://mp.weixin.qq.com/s/l1VHByLffFRrIfSwjKArmA', desc: 'AI智能眼，你的位置在我眼里。'},
      { titleWords: '黑科技', image: 'https://www.daikin1995.com/upload/2020/9/20200904113312.png', link: 'https://mp.weixin.qq.com/s/rrAFUtS6Hqj7ob6wuMYqdg', desc: '3分钟解锁大金室外机黑科技。'},
      { titleWords: '懒人必备', image: 'https://www.daikin1995.com/upload/2020/6/202061111263.png', link: 'https://mp.weixin.qq.com/s/98ZpitpED_K6hQMUc4XZVw', desc: '一台会自己洗澡的空调，懒人必备。'},
      { titleWords: '空气净化', image: 'https://www.daikin1995.com/upload/2020/8/2020829103411.png', link: 'https://mp.weixin.qq.com/s/vLjOdzSrBKKkOhHxf1PdHQ', desc: '关于新风系统，你必须知道的事。'},
      { titleWords: '家用中央空调', image: 'https://www.daikin1995.com/upload/2020/6/202061111265.png', link: 'https://mp.weixin.qq.com/s/mNBuGWBqpiMqjG3qEBEAXw', desc: '破解家用中央空调的五大疑问。'},
      { titleWords: '颜值担当', image: 'https://www.daikin1995.com/upload/2020/6/202061120591.png', link: 'https://mp.weixin.qq.com/s/ylANBuwDPpYA8hBQ6nhw7A', desc: '颜值担当，空调和灯槽我都想要。'},
      { titleWords: '远程操控', image: 'https://www.daikin1995.com/upload/2020/6/202061111267.png', link: 'https://mp.weixin.qq.com/s/kLECZW2s-GuPoHvZM9YvGw', desc: '选择金制家中的N个理由。'},
      { titleWords: '告别空调病', image: 'https://www.daikin1995.com/upload/2020/6/202061111268.png', link: 'https://mp.weixin.qq.com/s/MxhG_sV1RLJbWdglgrZr2g', desc: '你不知道吗 吹了一身的空调病有救了。'},
      { titleWords: '家用分体空调', image: 'https://www.daikin1995.com/upload/2020/6/202061111269.png', link: 'https://mp.weixin.qq.com/s/C5wBbRd1l9y0CHY9iC7b6Q', desc: '角落主人--悬角卫士守护你的洁净。'},
      { titleWords: '家有萌娃', image: 'https://www.daikin1995.com/upload/2020/6/2020611112610.png', link: 'https://mp.weixin.qq.com/s/Pi9L-V0yyvJYhDPXiT4eTg', desc: '家里有娃之后 觉得这些说的都挺对。'},
      { titleWords: '空气可视化', image: 'https://www.daikin1995.com/upload/2020/6/2020611112611.png', link: 'https://mp.weixin.qq.com/s/C3Epbaohh_m8YsPcLiClYQ', desc: '视不可挡，你的空气都能看得见。'},
      { titleWords: '自动除湿', image: 'https://www.daikin1995.com/upload/2020/6/2020611112612.png', link: 'https://mp.weixin.qq.com/s/PPVjbmyJDWjkXCoCWgH-yw', desc: '还在为除湿发愁，你out了。'},  
      { titleWords: '引入新风', image: 'https://www.daikin1995.com/upload/2020/6/2020611112613.png', link: 'https://mp.weixin.qq.com/s/gOadM1b6ne2HI3XNYP-DIQ', desc: '新风系统--守护你的纯净呼吸。'}
    ],
    isExtend:true,
    scrollTop:0,
    popImage:'',
    popLink:'',
  },
  
  onLoad: function (options) {
    console.log('index onLoad', options)

    this.getIndexConfig();
    
    let item = this.data.hotSearch[this.data.selectHotIndex];
    this.setData({ selectItem: item });

    let pop = this.selectComponent("#pop");
    api.getPopWindow().then(res => {
      let { result } = res;
      if (result) {
        // console.log(result)
        this.setData({ 
          popImage: result.image,
          popLink: result.link
          },()=>{
            pop.show();
            app.globalData.isPop = true;
          })
      }
    });

    let luckydraw = this.selectComponent("#luckydraw");
    luckydraw.show();
  },

  getPhoneNumber:function(e){
    // console.log(e);
    if (e.detail.iv) {
      let self = this;
      common.savePhoneNumber(e.detail, function(){
        self.setData({
          userLevel: userInfoStore.userInfo.level
        },() => {
          self.joinLuckydraw();
        });
      });
    }
  },

  joinLuckydraw:function(){
    let luckydraw = this.selectComponent("#luckydraw");
    let success = this.selectComponent("#success");
    let self = this
    api.participantIn({
      activityId: 4,
      userId: userInfoStore.userInfo.id,
      phone: userInfoStore.userInfo.phone
    }).then(res => {
      if(res.success && res.result && res.result.imageUrl){
        // self.setData({ successImage: res.result.imageUrl}, () => {
        //   success.show();
        // }) 
        wx.previewImage({
          urls: [res.result.imageUrl],
        })

      }
    },err => {
      // luckydraw.hide();
    });
    
  },

  save:function(){
    wx.saveImageToPhotosAlbum({
      filePath: this.data.successImage,
      success(res) {
        wx.showToast({
          title: '已保存到相册~',
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('index onShow', app.globalData)
    this.setData({
      userLevel: userInfoStore.userInfo.level,
      menuButtonClientRect: app.globalData.menuButtonClientRect,
      navigationBarClientRect: app.globalData.navigationBarClientRect,
      windowClientRect: app.globalData.windowClientRect
    })
    let giftBag = this.selectComponent("#giftbag");
    app.displayGiftBag(giftBag);

  },

  searchHot:function(e){
    let index = e.currentTarget.dataset.index;
    let item = this.data.hotSearch[index];
    this.setData({ selectHotIndex: index, selectItem: item});

    if(this.data.scrollTop<250){
      wx.pageScrollTo({
        scrollTop: 250,
        duration: 300
      });
    }
  },

  onPageScroll: function (ev) {
    this.setData({
      scrollTop: ev.scrollTop
    })
  },

  set:function(e){
    this.setData({
      isExtend: !this.data.isExtend
    });
  },

  showDetail:function(e){
    let url = this.data.selectItem.link;
    wx.navigateTo({
        url: 'webView/index?url=' + encodeURI(url),
    })
  },

  /**
   * 获取banner
   */
  getIndexConfig:function(){
    let self = this
    api.getbanner({ pageNo: 1 }).then(res => {
      self.setData({
        banners: res.result
      })
    })
    api.getIndexBg({}).then(res => {
      self.setData({
        bgUrl: res.result
      })
    })
    api.getIndexVideo({}).then(res => {
      self.setData({
        videoUrl: res.result.videoUrl,
        videoTitle: res.result.title
      })
    })
    api.getTwoCol({}).then(res => {
      self.setData({
        twoCol: res.result
      })
    })
    api.getFourCircle({}).then(res => {
      self.setData({
        fourCircle: res.result
      })
    })

    api.getHotSearch({}).then(res => {
      let idx = 0;
      for (let i = 0; i < res.result.length;i++){
        if (res.result[i].isDefault==1){
          idx = i;
          break;
        }
      }
      
      self.setData({
        hotSearch: res.result,
        selectHotIndex: idx,
        selectItem: res.result[idx]
      })
    })

  },

  gointro:function(e){
    let { item } = e.currentTarget.dataset;
    if (item.link && item.link.indexOf('https://')==0){
      wx.navigateTo({
        url: 'webView/index?url=' + encodeURIComponent(item.link),
      })
    }

    if (item.link && item.link.indexOf('/pages/') == 0) {
      let first = '/pages/coupon/index,/pages/mall/index,/pages/show/index,/pages/clickMe/index,';
      if (first.indexOf(item.link) != -1) {
        wx.switchTab({
          url: item.link,
        })
      } else {
        wx.navigateTo({
          url: item.link,
        })
      }
    }

      // wx.navigateTo({
      //   url: 'activityIntro/index?actid=' + item.actid
      // })

      //   let url = 'https://www.daikin1995.com/upload/decorate/decorate_7.png';
      //   let title = '全屋新风';
      //   wx.navigateTo({
      //     url: 'imagePage/index?title=' + title + '&url=' + url
      //   })

      // wx.scanCode({
      //   success: (res) => {
      //     console.log(res);
      //     if(res.path)
      //     wx.showModal({
      //       title: '扫码',
      //       content: res.path,
      //     })
      //   }
      // })
      
  },

  goschool:function(){
    wx.navigateTo({
      url: 'airSchool/index',
    })
  },

  goDetail:function(e){
    var index = e.currentTarget.dataset.index;
    if(index==0){
      wx.navigateTo({
        url: 'decorate/decorate',
      })
    }

    if (index == 1) {
      wx.navigateTo({
        url: 'decorated/decorated',
      })
    }
  },

  gonext:function(e){
    var index = e.currentTarget.dataset.index;
    if (index == 0) {
      wx.navigateTo({
        url: 'liuzi/liuzi',
      })
    }
    if (index == 1) {
      wx.navigateTo({
        url: 'activity/index',
      })
    }
    if (index == 2) {
      wx.switchTab({
        url: 'coupon/index',
      })
    }
    if (index == 3) {
      wx.switchTab({
        url: 'show/index',
      })
    }
  },

  bindVideoEnded: function () {
    var videoContext = wx.createVideoContext('videoBox');
    videoContext.seek(0);
    videoContext.pause();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().share();
  }
})